package com.cn.qgx428.orderingmeal.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.security.keystore.KeyInfo;
import android.support.annotation.BoolRes;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.cn.qgx428.orderingmeal.Home;
import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.animation.FoodToCar;
import com.cn.qgx428.orderingmeal.myClass.Food;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;

import java.util.zip.Inflater;

/**
 * Created by QinX on 2017/5/3.
 * 用户店铺中食物添加到购物车的逻辑窗口操作
 */

public class FoodSMInfo extends Dialog implements View.OnClickListener {

    private static final String TAG = "FoodMInfo";

    private Context context;

    private MyKeyInfo keyInfo;

    private TextView substractBtn;
    private TextView addBtn;
    private TextView amount;
    private TextView submit;
    private TextView cancel;
    private TextView foodName;

    private DialogInterface.OnClickListener dListener;

    private static final int SUB_AMOUNT = 0;
    private static final int ADD_AMOUNT = 1;

    public FoodSMInfo(@NonNull Context context, MyKeyInfo keyInfo) {
        super(context);
        this.context = context;
        this.keyInfo = keyInfo;
        setFoodSMInof();

    }

    private void setFoodSMInof() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.shop_foodsminfo_dialog, null);
        init(view);
        setContentView(view);
    }

    private void init(View view) {
        substractBtn = (TextView) view.findViewById(R.id.food_foodsminfo_substract);
        addBtn = (TextView) view.findViewById(R.id.food_foodsminfo_add);
        amount = (TextView) view.findViewById(R.id.food_foodsminfo_amount);
        submit = (TextView) view.findViewById(R.id.food_foodsminfo_submit);
        cancel = (TextView) view.findViewById(R.id.food_foodsminfo_cannel);
        foodName = (TextView) view.findViewById(R.id.food_foodsminfo_foodName);

        foodName.setText(keyInfo.getNowFoodName());

        substractBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        submit.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.food_foodsminfo_substract:
                changeAmount(SUB_AMOUNT);
                break;

            case R.id.food_foodsminfo_add:
                changeAmount(ADD_AMOUNT);
                break;

            case R.id.food_foodsminfo_submit:
                break;

            case R.id.food_foodsminfo_cannel:
                dismiss();
                break;
        }
    }

    private void changeAmount(int subAmount) {
        int number = Integer.parseInt(amount.getText().toString());

        if (subAmount == SUB_AMOUNT){
            if (number <= 1){
                amount.setText("1");
            } else {
                amount.setText(Integer.toString(number-1));
            }
        }

        if (subAmount == ADD_AMOUNT){
            Log.d(TAG, "changeAmount: "+number);
            amount.setText(Integer.toString(number+1));
        }
    }

    public void setSubmit(View.OnClickListener listener){
        submit.setOnClickListener(listener);
      ;
    }

    public int getFoodNumber(){
        return Integer.parseInt(amount.getText().toString());
    }
}
